package com.wwwarehouse.carddesk.fragment.teamfragment.checkdownclassfragemnt;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.animation.Animation;
import android.widget.GridView;

import com.alibaba.fastjson.JSON;
import com.wwwarehouse.carddesk.R;
import com.wwwarehouse.carddesk.adapter.teamadapter.DelayTaskAdapter;
import com.wwwarehouse.carddesk.bean.teambean.DelayTaskBean;
import com.wwwarehouse.carddesk.constant.CardDeskConstant;
import com.wwwarehouse.common.activity.base.BaseFragment;
import com.wwwarehouse.common.bean.response.CommonClass;
import com.wwwarehouse.common.custom_widget.state_layout.StateLayout;
import com.wwwarehouse.common.http.NoHttpUtils;
import com.wwwarehouse.common.tools.ConvertUtils;
import com.wwwarehouse.common.tools.LogUtils;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @创建者 chenchao.
 * @创建时间 2017/11/13 13:43
 * @描述 ${TODO}
 */

public class DelayTaskFragmentChild extends BaseFragment {

    private View                mView;
    private StateLayout         mStateLayout;
    private GridView            mGridView;
    private int                 mPage;
    private Map<String, Object> mDelayMap;
    private String              mUserId;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mView = inflater.inflate(R.layout.fragment_delay_task_child, container, false);
        initView();
        initData();
        return mView;
    }

    private void initData() {

        if (getArguments() != null) {
            Bundle bundle = getArguments();
            mPage = bundle.getInt("page");
            mUserId = bundle.getString("userId");
            mDelayMap = new HashMap();
            mDelayMap.put("opUserId", mUserId);
            mDelayMap.put("page", mPage);
            mDelayMap.put("size", 9);
            getDelayTask();

        }


    }
    public void getDelayTask() {
        mStateLayout.setAllContentViewMargin(ConvertUtils.dip2px(mActivity,11),ConvertUtils.dip2px(mActivity,5),ConvertUtils.dip2px(mActivity,11),ConvertUtils.dip2px(mActivity,0));
        mStateLayout.showProgressView(true);
        NoHttpUtils.httpPost(CardDeskConstant.GET_DELAY_DATA, mDelayMap, mOnResponseListener, 0);
    }
    private NoHttpUtils.OnResponseListener mOnResponseListener = new NoHttpUtils.OnResponseListener(){
        @Override
        public void onSucceed(CommonClass commonClass, int what) {
            try {
                switch (what) {
                    case 0:
                        if ("0".equals(commonClass.getCode())) {
                            DelayTaskBean delayTaskBean = JSON.parseObject(commonClass.getData().toString(), DelayTaskBean.class);
                            List<DelayTaskBean.ListBean> list = delayTaskBean.getList();
                            DelayTaskAdapter delayTaskAdapter = new DelayTaskAdapter(mActivity, list, mGridView);
                            mGridView.setAdapter(delayTaskAdapter);
                        }

                        break;
                    default:
                        break;
                }
            } catch (Exception e) {
                LogUtils.showLog(e.toString());
            }
        }

        @Override
        public void onFailed(String result, int what) {
            mStateLayout.showNetworkView(false);
            mStateLayout.setNetworkListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    getDelayTask();
                }
            });
        }

        @Override
        public void onFinish(int what) {
            mStateLayout.showContentView();
        }
    };

    private void initView() {

        mStateLayout = findView(mView, R.id.sl_layout);
        mGridView = findView(mView, R.id.gv);
    }

    @Override
    public void setTitle() {
        if (peekFragment() instanceof DelayTaskFragmentChild) {
            mActivity.setTitle(R.string.teamdesk_delay_detail);
        }
    }

    @Override
    public void requestDatas() {

    }

    @Override
    public Animation onCreateAnimation(int transit, boolean enter, int nextAnim) {
        return null;
    }



}
